﻿package com.toncen.samepms.system.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.toncen.samepms.common.AbstractDatabaseAction;
import com.toncen.samepms.interceptor.In;
import com.toncen.samepms.interceptor.Out;
import com.toncen.samepms.system.dao.CodeDao;
import com.toncen.samepms.system.dao.CodeItemDao;
import com.toncen.samepms.system.domain.Code;
import com.toncen.samepms.system.domain.CodeItem;
import com.sky.common.http.HTTPUtils;
import com.sky.common.json.JSONUtils;

/**
 * 描述：编码明细表SYSTEM_CODE_ITEM的action文件
 * 
 * @author tch
 * 
 */
public class CodeItemAction extends AbstractDatabaseAction<CodeItem, CodeItemDao> {
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1331288513294L;

	/**
	 * 
	 */
	@In
	@Out
	private List<Code> codeList = new ArrayList<Code>();

	/**
	 * @return the codeList
	 */
	public List<Code> getCodeList() {
		return codeList;
	}

	/**
	 * @param codeList
	 *            the codeList to set
	 */
	public void setCodeList(List<Code> codeList) {
		this.codeList = codeList;
	}

	public CodeItemAction() throws Exception {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#prepareOnDoEdit()
	 */
	@Override
	public void prepareOnDoEdit() throws Exception {
		super.prepareOnDoEdit();
		setCodeList(new CodeDao().getAll());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#prepareOnDoNew()
	 */
	@Override
	public void prepareOnDoNew() throws Exception {
		super.prepareOnDoNew();
		setCodeList(new CodeDao().getAll());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#prepareOnDoView()
	 */
	@Override
	public void prepareOnDoView() throws Exception {
		super.prepareOnDoView();
		setCodeList(new CodeDao().getAll());
	}

	/**
	 * create:xl create date:2012-5-29下午07:07:14 方法描述： 通过编码查询对应的详细编码
	 */
	public void onDoGetByCodeValue() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String codeValue = HTTPUtils.getRequestParameter(request, "codeValue");
		CodeDao codeDao = new CodeDao();
		List<CodeItem> list = codeDao.getByCode(codeValue).getCodeItemList();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.getWriter().flush();
		response.getWriter().println(JSONUtils.object2JSON(list));
		// JsonUtil.toJson(list, getResponse().getWriter());
		response.getWriter().close();
	}
}
